package frontend;
import java.awt.BorderLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.sql.SQLException;
import java.util.ArrayList;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;

import javax.swing.WindowConstants;
import javax.swing.SwingUtilities;

import dbmanager.RecipeManager;

import framework.FoodItem;
import framework.Ingredient;
import framework.Recipe;
import framework.Utensil;


/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
public class NewRecipeFrame extends javax.swing.JFrame {

	{
		//Set Look & Feel
		try {
			javax.swing.UIManager.setLookAndFeel(javax.swing.UIManager.getSystemLookAndFeelClassName());
		} catch(Exception e) {
			e.printStackTrace();
		}
	}

	private JLabel addRecipeLabel;
	private JScrollPane jScrollPane2;
	private JLabel jLabel4;
	private JTextField titleField;
	private JScrollPane jScrollPane1;
	private JButton jButton1;
	private JButton submitButton;
	private JLabel jLabel3;
	private JLabel jLabel2;
	private JLabel jLabel1;
	private JLabel stepsLabel;
	private JScrollPane jScrollPane3;
	private JTextArea stepsArea;
	private JTextArea utensilArea;
	private JTextArea ingredientArea;

	/**
	* Auto-generated main method to display this JFrame
	*/
	public static void main(String[] args) {
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				NewRecipeFrame inst = new NewRecipeFrame();
				inst.setLocationRelativeTo(null);
				inst.setVisible(true);
			}
		});
	}
	
	public NewRecipeFrame() {
		super();
		initGUI();
	}
	
	private void initGUI() {
		try {
			setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
			getContentPane().setBackground(new java.awt.Color(255,255,255));
			getContentPane().setLayout(null);
			this.setTitle("Submit a New Recipe");
			this.setResizable(false);
			{
				addRecipeLabel = new JLabel();
				getContentPane().add(addRecipeLabel);
				addRecipeLabel.setText("Add a New Recipe");
				addRecipeLabel.setBounds(19, 12, 294, 45);
				addRecipeLabel.setFont(new java.awt.Font("Bitstream Vera Sans",1,16));
			}
			{
				jScrollPane2 = new JScrollPane();
				getContentPane().add(jScrollPane2);
				jScrollPane2.setBounds(19, 418, 232, 124);
				{
					utensilArea = new JTextArea();
					jScrollPane2.setViewportView(utensilArea);
					utensilArea.setBounds(19, 349, 232, 193);
					utensilArea.setBorder(BorderFactory.createTitledBorder(""));
				}
			}
			{
				jScrollPane3 = new JScrollPane();
				getContentPane().add(jScrollPane3);
				jScrollPane3.setBounds(274, 79, 405, 463);
				{
					stepsArea = new JTextArea();
					jScrollPane3.setViewportView(stepsArea);
					stepsArea.setBounds(274, 79, 405, 463);
					stepsArea.setBorder(BorderFactory.createTitledBorder(""));
					{
						submitButton = new JButton();
						stepsArea.add(submitButton);
						submitButton.setText("Submit Recipe");
						submitButton.setPreferredSize(new java.awt.Dimension(242, 208));
					}
				}
			}
			{
				stepsLabel = new JLabel();
				getContentPane().add(stepsLabel);
				stepsLabel.setText("Steps");
				stepsLabel.setBounds(274, 48, 70, 27);
				stepsLabel.setFont(new java.awt.Font("Bitstream Vera Sans",1,12));
			}
			{
				jLabel1 = new JLabel();
				getContentPane().add(jLabel1);
				jLabel1.setText("Ingredients");
				jLabel1.setFont(new java.awt.Font("Bitstream Vera Sans",1,12));
				jLabel1.setBounds(19, 117, 147, 27);
			}
			{
				jLabel2 = new JLabel();
				getContentPane().add(jLabel2);
				jLabel2.setText("Utensils");
				jLabel2.setFont(new java.awt.Font("Bitstream Vera Sans",1,12));
				jLabel2.setBounds(19, 383, 78, 27);
			}
			{
				jLabel3 = new JLabel();
				getContentPane().add(jLabel3);
				jLabel3.setText("<html>Separate ingedients, utensils and steps by commas.<br>Ingredients should be in the form of (#) (unit) (name)");
				jLabel3.setFont(new java.awt.Font("Bitstream Vera Sans",1,12));
				jLabel3.setBounds(19, 554, 387, 27);
			}
			{
				jButton1 = new JButton();
				getContentPane().add(jButton1);
				jButton1.setText("Submit Recipe");
				jButton1.setBounds(475, 554, 197, 28);
				jButton1.addMouseListener(new MouseAdapter() {
					public void mouseClicked(MouseEvent evt) {
						try {
							jButton1MouseClicked(evt);
						} catch (SQLException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
					}
				});
			}
			{
				jScrollPane1 = new JScrollPane();
				getContentPane().add(jScrollPane1);
				jScrollPane1.setBounds(19, 150, 230, 227);
				{
					ingredientArea = new JTextArea();
					jScrollPane1.setViewportView(ingredientArea);
					ingredientArea.setBounds(19, 150, 230, 227);
					ingredientArea.setBorder(BorderFactory.createTitledBorder(""));
				}
			}
			{
				titleField = new JTextField();
				getContentPane().add(titleField);
				titleField.setBounds(19, 86, 230, 25);
			}
			{
				jLabel4 = new JLabel();
				getContentPane().add(jLabel4);
				jLabel4.setText("Title");
				jLabel4.setFont(new java.awt.Font("Bitstream Vera Sans",1,12));
				jLabel4.setBounds(19, 56, 70, 27);
			}
			pack();
			this.setSize(706, 630);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	private void jButton1MouseClicked(MouseEvent evt) throws SQLException {
		ArrayList<Ingredient> ing = new ArrayList<Ingredient>();
		ArrayList<Utensil> ut = new ArrayList<Utensil>();
		String [] utensils = utensilArea.getText().split(", ");
		for(int i=0;i<utensils.length;i++){
			ut.add(new Utensil(utensils[i]));
		}
		String[] ingredients = ingredientArea.getText().split(",");
		for(int i=0;i<ingredients.length;i++){
			String[] temp = ingredients[i].split(" ");
			ing.add(new Ingredient(new FoodItem(temp[2]), temp[1], Integer.parseInt(temp[0])));
		}
		Recipe r = new Recipe(titleField.getText(), stepsArea.getText().replace(",", "\\n"), ing, ut);
		
		RecipeManager.submitRecipe(r);
		this.dispose();
	}

}
